package com.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.base.action.BaseAction;
import com.model.Message;
import com.model.User;
import com.opensymphony.xwork2.ModelDriven;
import com.service.IMessageService;
import com.util.PageUtil;

public class MessageAction extends BaseAction implements ModelDriven<Message> {

	private static final long serialVersionUID = 6234179329182126893L;
	
	private IMessageService messageService;
	private Message message;
	
	int page;
	HttpServletRequest request;

	public void setMessageService(IMessageService messageService) {
		this.messageService = messageService;
	}
	
	
	public Message getMessage() {
		return message;
	}


	public void setMessage(Message message) {
		this.message = message;
	}


	@Override
	public Message getModel() {
		return message;
	}	
	
	public String getAll(){
		PageUtil pageModel = new PageUtil();
		// 获得当前页
		if ((String) ServletActionContext.getRequest().getParameter("page") == null) {
			pageModel.setPage(1);
		} else {
			page = Integer.parseInt((String) ServletActionContext.getRequest()
					.getParameter("page"));
			if (page != 0) {
				pageModel.setPage(page);
			}
		}
		request = ServletActionContext.getRequest();
		String ft=request.getParameter("findType");
		if(ft==null){
			ft=(String)this.getSession().get("findType");
			if(ft==null)ft="all";
		}else this.getSession().put("findType",ft);
		String tj="";
		User u=(User) this.getSession().get("loginUser");
		if(u!=null)
			if("my".equals(ft)) tj="where user.id="+u.getId();
		pageModel.setPageSize(3);// 设置页面显示最大 值
		pageModel.setTotalCount(messageService.count("select count(*) from Message "+tj)); // 数据总条数
		pageModel.setNum(3); // 设置当前页的前后距离
		// 通过当前页和
		List<Message> aboutList = (List<Message>) messageService.getAll("from Message "+tj+" order by id desc", page, 3);
		pageModel.setItems(aboutList);
		request.setAttribute("count", aboutList.size());// 放置在request中
		request.setAttribute("pageModel", pageModel);
		request.setAttribute("page", pageModel.getPage());
		return "getAll";
	}
	
	public String add(){
		message.setCreatedTime(new Date());
		message.setUser((User) this.getSession().get("loginUser"));
		messageService.saveOrUpdate(message);
		return "add";
	}

	public String del(){
		Integer id=Integer.parseInt(this.getRequest().getParameter("id"));		
		messageService.delete(id);
		return "del";
	}
	public String get(){
		String idStr=this.getRequest().getParameter("id");
		if(idStr!=null && idStr.length()!=0 && !idStr.equals("")){
			Integer id=Integer.parseInt(idStr);		
			Message model=messageService.get(id);		
			this.getRequest().setAttribute("model", model);						
		}
		return "get";
	}
	public String edit(){
		messageService.update(message);
		return "edit";
	}

	public String checkSession(){
		User user=(User) this.getSession().get("loginUser");
		if(user==null){
			return "toLogin";
		}else{
			return "toAdd";
		}
	}
}
